package com.seo.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 权限处理
 * 
 */
public class Jurisdiction {

	public static boolean qxs(String menuUrls) {

		boolean res = false;
		for (String menuUrl : menuUrls.split(",")) {
			if (qx(menuUrl)) {
				return true;
			}
		}
		return res;
	}

	public static boolean qxs(String menuUrls, String type) {

		boolean res = false;
		for (String menuUrl : menuUrls.split(",")) {
			if (qx(menuUrl, type)) {
				return true;
			}
		}
		return res;
	}

	/**
	 * 访问权限及初始化按钮权限(控制按钮的显示 增删改查)
	 * 
	 * @param menuUrl
	 *            菜单路径
	 * @return
	 */
	public static boolean qx(String menuUrl) {
		return qx(menuUrl, "cha");
	}

	@SuppressWarnings("unchecked")
	public static boolean qx(String menuUrl, String type) {
		HttpSession session = getSession();
		String userType = getT("userType", String.class);
		if ("01".equals(userType)) {
			if ("cha".equals(type)) {
				Map<String, Boolean> qx = new HashMap<>();
				qx.put("add", true);
				qx.put("del", true);
				qx.put("edit", true);
				getRequest().setAttribute("QX", qx);
			}
			return true;
		} else {
			List<Map<String, Object>> menuList = (List<Map<String, Object>>) session
					.getAttribute(Const.SESSION_MENULIST);
			Map<String, Object> role = (Map<String, Object>) session.getAttribute(Const.SESSION_ROLE);
			return readMenu(menuList, menuUrl, role, type);
		}
	}

	@SuppressWarnings("unchecked")
	public static boolean webQx(String menuUrl, String type) {
		HttpSession session = getSession();

		List<Map<String, Object>> menuList = (List<Map<String, Object>>) session.getAttribute(Const.SESSION_MENULIST);
		Map<String, Object> role = (Map<String, Object>) session.getAttribute(Const.SESSION_ROLE);
		return readMenu(menuList, menuUrl, role, type);

	}

	public static boolean readMenu(List<Map<String, Object>> menuList, String menuUrl, Map<String, Object> role,
			String type) {
		for (Map<String, Object> menu : menuList) {
			String url = (String) menu.get("url");
			if (url.equals(menuUrl)) {
				boolean selected = (boolean) menu.get("selected");
				if (!selected) {
					return false;
				} else {

					if ("cha".equals(type)) {
						Map<String, Boolean> qx = new HashMap<>();
						qx.put("add", RightsHelper.testRights((String) role.get("addQx"), (Integer) menu.get("id")));
						qx.put("del", RightsHelper.testRights((String) role.get("delQx"), (Integer) menu.get("id")));
						qx.put("edit", RightsHelper.testRights((String) role.get("editQx"), (Integer) menu.get("id")));
						getRequest().setAttribute("QX", qx);
						return RightsHelper.testRights((String) role.get("chaQx"), (Integer) menu.get("id"));
					} else {
						return RightsHelper.testRights((String) role.get(type + "Qx"), (Integer) menu.get("id"));
					}
				}

			}
		}
		return false;

	}

	/**
	 * 获取当前登录的用户名
	 * 
	 * @return
	 */
	public static String getUsername() {
		return getSession().getAttribute(Const.SESSION_USERNAME).toString();
	}

	/**
	 * 获取当前按钮权限(增删改查)
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getUser() {
		return (Map<String, Object>) getSession().getAttribute(Const.SESSION_USER);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> getUser(String sessionCode) {
		return (Map<String, Object>) getSession().getAttribute(sessionCode);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getT(String key, Class<T> type) {
		Map<String, Object> sysUser = (Map<String, Object>) getSession().getAttribute(Const.SESSION_USER);
		return (T) sysUser.get(key);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getT(String key, Class<T> type, String sessionCode) {
		Map<String, Object> obj = (Map<String, Object>) getSession().getAttribute(sessionCode);
		return (T) obj.get(key);
	}
	
	public static boolean roleCode(String code){
		String codeR = getT("roleCode", String.class);
		return code.equals(codeR);
	}
	
	/**
	 * shiro管理的session
	 * 
	 * @return
	 */
	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}
}
